package com.yugao.fintech.antelope.mps.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.antelope.mps.core.propcess.MsgPushProcess;
import com.yugao.fintech.antelope.mps.model.rqrs.history.HistoryPageReq;
import com.yugao.fintech.antelope.mps.model.rqrs.history.HistoryQueryResp;
import com.yugao.fintech.antelope.mps.service.MsgHistoryService;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.framework.core.response.Resp;
import com.yugao.fintech.framework.samples.ratelimit.LimitTypeEnum;
import com.yugao.fintech.framework.samples.ratelimit.RateLimit;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/msgHistory")
@Api(tags = "消息推送")
public class MsgHistoryController {
    @Autowired
    private MsgPushProcess msgPushHandler;
    @Autowired
    private MsgHistoryService msgHistoryService;

    /**
     * 分页 消息推送
     */
    @RamResource(action = "msg:history:list", name = "查询消息推送记录列表", type = ActionType.READ)
    @GetMapping("/page")
    public Resp<IPage<HistoryQueryResp>> pageHistory(@Validated HistoryPageReq req) {
        return Resp.ok(msgHistoryService.pageHistory(req));
    }

    /**
     * 通过id查找 消息推送
     */
    @RamResource(action = "msg:history:get", name = "查询消息推送记录", type = ActionType.READ)
    @GetMapping("/get")
    public Resp<HistoryQueryResp> getById(@RequestParam(value = "id") Long id) {
        return Resp.ok(msgHistoryService.getById(id));
    }

    /**
     * 重新推送消息, 需要做防刷操作
     * @param id 消息id
     */
    @PostMapping("/rePush")
    @RamResource(action = "msg:history:rePush", name = "重新推送消息", type = ActionType.WRITE)
    @RateLimit(limitType = LimitTypeEnum.IP, time = 10, count = 2)
    public Resp<?> rePush(@RequestParam(value = "id") Long id) throws Exception {
        msgHistoryService.rePush(id);
        return Resp.ok();
    }

    /**
     * 批量删除 消息推送
     */
    @RamResource(action = "msg:history:delete", name = "删除消息推送记录", type = ActionType.WRITE)
    @DeleteMapping("/batchDelete")
    public Resp<?> deleteBatch(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(msgHistoryService.deleteByIds(ids));
    }

    /**
     * 删除 消息推送
     */
    @RamResource(action = "msg:history:delete", name = "删除消息推送记录", type = ActionType.WRITE)
    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestParam(value = "id") Long id) {
        return Resp.ok(msgHistoryService.deleteById(id));
    }
}
